<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%article}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $keyword
 * @property string $action
 * @property string $picture
 * @property integer $status
 * @property string $description
 * @property string $content
 * @property integer $admin_id
 * @property integer $updated_at
 * @property integer $created_at
 */
class Article extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%article}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['status', 'admin_id', 'updated_at', 'created_at','cate_id','menu_id','is_top','sort_order','type_id'], 'integer'],
            [['description', 'content'], 'string'],
            [['title', 'keyword'], 'string', 'max' => 255],
            [['picture'], 'string', 'max' => 200],
            [['title', 'menu_id', 'description','content'], 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'title' => Yii::t('zhtfrontend', '标题'),
            'keyword' => Yii::t('zhtfrontend', '关键词'),
            'cate_id' => Yii::t('zhtfrontend', '分类id'),
            'menu_id' => Yii::t('zhtfrontend', '模块'),
            'action' => Yii::t('zhtfrontend', '方法'),
            'picture' => Yii::t('zhtfrontend', '图片'),
            'status' => Yii::t('zhtfrontend', '状态'),
            'description' => Yii::t('zhtfrontend', '简介'),
            'content' => Yii::t('zhtfrontend', '详情'),
            'admin_id' => Yii::t('zhtfrontend', '管理员id'),
            'is_top' => Yii::t('zhtfrontend', '是否置顶 '),
            'sort_order' => Yii::t('zhtfrontend', '排序'),
            'updated_at' => Yii::t('zhtfrontend', '编辑时间'),
            'created_at' => Yii::t('zhtfrontend', '添加时间'),
            'type_id' => Yii::t('zhtfrontend', '三大类'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['status'] = function () {
            switch ($this->status) {
                case 1:
                    return \Yii::t('zhtfrontend', '显示');
                case 2:
                    return \Yii::t('zhtfrontend', '不显示');
            }
        };
        $fields['is_top'] = function () {
                    switch ($this->is_top) {
                        case 0:
                            return \Yii::t('zhtfrontend', '否');
                        case 1:
                            return \Yii::t('zhtfrontend', '是');
                    }
                };

        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'menu_name' => function(){
            if(!empty($this->navmenu)){
                if($this->navmenu->type_id == 1){
                    $typename = '益智';
                }elseif ($this->navmenu->type_id == 2){
                    $typename = '音乐';
                }else{
                    $typename = '形体';
                }
                return $this->navmenu->name.'-'.$typename;
            }

            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function getNavmenu(){
        return $this->hasOne(NavMenu::className(),['id'=>'menu_id']);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}
